package pert;



import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @(#) createDot.java
 */

public class createDot
{
	private hw3Pert Trigger;
	
	public void makeDOT(List <Task> taskList )
	{
   
   // This can be made user configurable path
		String file="output.dot";//"E:\\SE-2\\SY-Modeling\\Assignments\\out.dot";
		try {
			
			FileWriter writer = new FileWriter(file);
			
			PrintWriter dotFile = new PrintWriter(writer);
			dotFile.println("digraph myPERT {");
				
			String nodeId = null,color,color2 = null,label;
			int pos = 0;
			int days = 0;
			String line=null,line2=null;
			int subl=0;
			
			int l=taskList.size();
			
			
			int nl=taskList.size();
			String task = null;
			taskList.get(0).setColor("red");
			for (int xl=0;xl<nl-1;xl++){
				int inl=taskList.get(xl).getNodes().size();
				int max=0;
				//System.out.println("Node id is "+taskList.get(xl).getId());
				if(taskList.get(xl).getNodes().size()>0){ 
					max=taskList.get(xl).getNodes().get(0).getDuration();
			
				for (int y=0;y<inl;y++){
					//System.out.println("DEpendnts are "+taskList.get(xl).getNodes().get(y).getId()+" Durations "+taskList.get(xl).getNodes().get(y).getDuration());
					int temp=taskList.get(xl).getNodes().get(y).getDuration();
					if (max<=temp){
						max=taskList.get(xl).getNodes().get(y).getDuration();
						
						task=taskList.get(xl).getNodes().get(y).getId();
					}else
						task=taskList.get(xl).getNodes().get(0).getId();
					
				}
				
				}
				
				//System.out.println("Critical is "+task);
				for (int yl=0;yl<nl;yl++){
					if(task.equals(taskList.get(yl).getId())){
						
						 pos=yl;
						
						taskList.get(yl).setColor("red");
						//System.out.println("Task id is "+taskList.get(yl).getId()+ "color is "+taskList.get(yl).getColor());
						pos=pos-1;									
						break;
					}
					
					
				}
				xl=pos;
				//System.out.println("xl is "+xl);
				
			}
			
						
			
			for(int nel=0;nel<nl;nel++){
			color=taskList.get(nel).getColor();
			nodeId=taskList.get(nel).getId();
			days=taskList.get(nel).getDuration();
			line=nodeId+"[shape=polygon, sides=4, style=\"bold\",color="+ color+",label=\""+nodeId+" "+days+"Day\" ]";
			dotFile.println(line);
			
			subl=taskList.get(nel).getNodes().size();
			for(int sl=0;sl<subl;sl++){
				if((taskList.get(nel).getColor().equals("red"))&&(taskList.get(nel).getNodes().get(sl).getColor().equals("red")))
					color2="red";
				else
					color2="black";
				line2=nodeId+"->"+taskList.get(nel).getNodes().get(sl).getId()+"[label=\"\", color=\""+color2+"\"]";
				dotFile.println(line2);
			}
			
			
			
			
			}
			
			
			
			dotFile.println("}");
			dotFile.close();
		}
			 catch(FileNotFoundException ex) {
			        System.out.println(
			            "Unable to open file '" + file + "'");				
			    }	
			
		 catch (IOException e) {
			
			e.printStackTrace();
		}	
		
	}
	}
	
	

